package com.learn.databinding.workwithobservabledataobjects

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.View.OnClickListener
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableArrayMap
import com.exercise.struct.base.BaseDataBindingActivity
import com.learn.databinding.R
import com.learn.databinding.databinding.ActivityWorkWithObservableDataObjectBinding
import com.learn.databinding.workwithobservabledataobjects.model.Fields
import com.learn.databinding.workwithobservabledataobjects.model.User
import com.learn.databinding.workwithobservabledataobjects.model.User2

class WorkWithObservableDataObjectActivity :
    BaseDataBindingActivity<ActivityWorkWithObservableDataObjectBinding>(),OnClickListener {
    override val layoutResId: Int
        get() = R.layout.activity_work_with_observable_data_object
    private val user = User().apply {
        firstName.set("花")
        lastName.set("木兰")
        age.set(18)
    }
    private val userMap = ObservableArrayMap<String, Any>().apply {
        put("firstName", "花")
        put("lastName", "木兰")
        put("age", 18)
    }
    private val userList = ObservableArrayList<Any>().apply {
        add("花")
        add("木兰")
        add(18)
    }

    private val user2 = User2().apply {
        firstName = "花"
        lastName = "木兰"
    }

    override fun setToolbar() {
        mToolbar.setTitle("WorkWithObservableDataObject")
    }

    override fun initView() {
        mDataBinding.mOnClickListener = this
        mDataBinding.user2 = user2
        mDataBinding.user = user
        mDataBinding.userMap = userMap
        mDataBinding.userList = userList
    }

    companion object {
        fun start(context: Context) {
            Intent(context, WorkWithObservableDataObjectActivity::class.java)
                .also {
                    context.startActivity(it)
                }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btnChangeData -> {
                //修改Observable Object
                user2.firstName = "张"
                //修改ObservableXXX
                user.firstName.set("张")
                //修改ObservableArrayMap中的数据
                userMap[Fields.FIRST_NAME] = "张"
                //修改ObservableArrayList中的数据
                userList[0] = "张"
            }
            else -> {}
        }
    }
}